# 本质上，decorator就是一个返回函数的高阶函数
import functools
import time

def log1(func):
    def wrapper(*args, **kw):
        print('call %s():' % func.__name__)
        return func(*args, **kw)

    return wrapper


def log(text):
    def decorator(func):
        ##保留原始方法名和文档，便于其他开发者理解代码
        @functools.wraps(func)
        def wrapper(*args, **kw):
            print('%s %s():' % (text, func.__name__))
            return func(*args, **kw)

        return wrapper

    return decorator


@log('execute')
def now():
    print('2025-05-21')

now()

# f = now
# print(f.__name__)
#
# now = log1(now)
# print(now.__name__)



def metric(fn):
    @functools.wraps(fn)
    def wrapper(*args, **kw):
        print('begin call')
        result = fn(*args, **kw)
        print('end call')
        print('%s executed in %s ms' % (fn.__name__, 10.24))
        return result
    return wrapper


# 测试
@metric
def fast(x, y):
    time.sleep(0.0012)
    return x + y


@metric
def slow(x, y, z):
    time.sleep(0.1234)
    return x * y * z


f = fast(11, 22)
s = slow(11, 22, 33)
if f != 33:
    print('测试失败!')
elif s != 7986:
    print('测试失败!')



